package atom.core0.app.thread.pool;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

public class MyPoolThreadFactory implements ThreadFactory
{
    static final AtomicInteger poolNumber = new AtomicInteger(1);
    final ThreadGroup group;
    final AtomicInteger threadNumber = new AtomicInteger(1);
    private String namePrefix;

    public String getNamePrefix()
    {
    	return namePrefix;
    }
	public void setNamePrefix(String namePrefix)
    {
    	this.namePrefix = namePrefix;
    }
	MyPoolThreadFactory() {
        SecurityManager s = System.getSecurityManager();
        group = (s != null)? s.getThreadGroup() :
                             Thread.currentThread().getThreadGroup();
        namePrefix = "MyPoolThread-" +
                      poolNumber.getAndIncrement() +
                     "-thread-";
    }
    MyPoolThreadFactory(String threadDesc) {
        SecurityManager s = System.getSecurityManager();
        group = (s != null)? s.getThreadGroup() :
                             Thread.currentThread().getThreadGroup();
        namePrefix = "MyPoolThread-" +threadDesc+"-"+
                      poolNumber.getAndIncrement() +
                     "-thread-";
    }
    public Thread newThread(Runnable r) {
        Thread t = new MyPoolThread(group, r,
                              namePrefix + "-" +threadNumber.getAndIncrement(),
                              0);
        if (t.isDaemon())
            t.setDaemon(false);
        if (t.getPriority() != Thread.NORM_PRIORITY)
            t.setPriority(Thread.NORM_PRIORITY);
        return t;
    }

}
